﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
namespace DBA.App.Dictionary
{
    public class GoogleImager
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        public static bool IsConnectedToInternet()
        {

            int Desc;
            return InternetGetConnectedState(out Desc, 0);

        }
        public static bool IsServerActive()
        {
            Ping p = new Ping();
            PingReply reply = p.Send("74.125.71.105", 200);
            if (reply.Status.ToString() == "Success")
                return true;
            return false;
        }
        public static bool Save(string strImageUrl, string strFileName)
        {
            if (strImageUrl == null || strFileName == null)
                return false;

            try
            {
                HttpWebRequest request =
               (HttpWebRequest)WebRequest.Create(strImageUrl);
                HttpWebResponse response;
                using (response =
                           (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream =
                              response.GetResponseStream())
                    {
                        Image img = Image.FromStream(responseStream);
                        if (img != null)
                        {
                            img.Save(strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }
                }
            }
            catch (WebException)
            {
                return false;
            }
            return true;
        }

        public static string GetImageUrl(string strSearchTerm)
        {

            string[] arrayUrls = null;

            if (strSearchTerm != string.Empty)
            {
                strSearchTerm.Replace(' ', '+');
                string strRequestUrl = "http://images.google.com/images?q="
                    + strSearchTerm;
                HttpWebRequest request =
                    (HttpWebRequest)WebRequest.Create(strRequestUrl);                
                HttpWebResponse response;                
                string resultPage = string.Empty;
                try
                {
                    using (response =
                               (HttpWebResponse)request.GetResponse())
                    {
                        using (Stream responseStream =
                                  response.GetResponseStream())
                        {
                            using (StreamReader reader =
                                     new StreamReader(responseStream))
                            {
                                resultPage = reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch (WebException) {
                    return string.Empty;
                }
               
                if (resultPage != string.Empty)
                {
                    string regex = "src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg))(?:\"|\')?";
                    arrayUrls = Regex.Split(resultPage, regex);                    
                }
            }

            if (arrayUrls.Length > 1 && arrayUrls != null)
                return arrayUrls[1];             
            
            return string.Empty;
        }

    }
}
